#version 460
#extension GL_GOOGLE_include_directive : enable

#include "../../../core/shader_color.h"
#include "../../../core/shader_tranform.h"
#include "../../../core/shader_widget_color.h"
#include "../../../core/shader_widget_panel.h"


layout(std430, column_major, binding = DEF_BINDING_WIDGET_TextCoordID) readonly buffer U4_Buf {
	highp int m_Text_ID[];
};

// (UV偏移)  (顶边偏移 宽度)  (Zuv偏移 )
layout(std430, column_major, binding = DEF_BINDING_WIDGET_TextCoord) readonly buffer U5_Buf {
	highp ivec4 m_Text_Coord[];
};


 

layout(points) in;
layout(points, max_vertices=64) out;


in gl_PerVertex {
	vec4	gl_Position;
	float	gl_PointSize;
} gl_in[];

layout(location = 1) in VS_GS_VERTEX {
	uint InstanceIndex;
}vertex_in[];




layout(location = 1) out VS_FS_VERTEX {
	vec4		gColor;
	flat ivec3	vUV_Offset;
	flat ivec2	vUV_Bound;
};


out gl_PerVertex {
	vec4	gl_Position;
	float	gl_PointSize;
};



void main(void){
	// 位置  大小  对齐方式  深度
	mat3x2 mat = m_LP_SD[vertex_in[0].InstanceIndex];
	
	
	vec4 TColor = f_glsl_buildColor(InstanceColor[vertex_in[0].InstanceIndex]);
	int strNumn = int(mat[1].y);
	int strStartOffset = int(mat[2].x);
	
	//mat3x2 fonts = m_Text_Coord[0];
	ivec4 fonts = m_Text_Coord[0];
	vec4 v = vec4(0.0, 0.0, mat[2].y, 1.0);
	
	if(strNumn > 128) strNumn = 0;

	//间隔步进
	float advance = 0;
	for(int f=0; f<strNumn; ++f){
		ivec4 fontsUV = m_Text_Coord[m_Text_ID[strStartOffset + f]];
		

		vUV_Offset.xy = fontsUV.xy;
		vUV_Offset.z = (fontsUV.z<<16)>>16;
		//vUV_Bound = vec2(fontsUV[1].x, fonts[0].y);
		vUV_Bound = ivec2(fontsUV.x, fonts.y);
		
		
		gl_Position = v;
		gl_Position.xy += mat[0];
		gl_Position.xy *= m_Scale;


		
		gl_Position.x += advance;
		gl_Position.xy += fonts.y * 0.5;
		gl_Position.y += fontsUV.w;

		

		gl_Position.xy += m_Loc;
		//gl_Position = vec4(100, 200, 0, 1.0);
		gl_Position = mat_ProjecView * gl_Position;
		//gl_PointSize = fonts.y * m_Scale.x;
		
		if(m_Scale.x < 1 || m_Scale.y < 1){
			gl_PointSize = fonts.y * m_Scale.x;
		}else{
			gl_PointSize = fonts.y;
		}
		
		
		advance += (fontsUV.z>>16) + 2;
		gColor = TColor;
		EmitVertex();
	}

	EndPrimitive();
	
	
}



 